
@extends('admin.common.main')

@section('cnt')
<style>
     label.error {
        left: 18px;
        font-size: 14px;
    }
</style>
    <article class="page-container">
        @include('admin.common.validate')
        <form action="{{ route('admin.admin-users.role', $admin) }}" method="post" class="form form-horizontal" id="form-admin-role-add">
            @csrf
            <div class="role">
                <div class="formControls col-xs-12 col-sm-12">
                    @foreach($roleAll as $key=>$item)
                        <div class="mt-20 skin-minimal">
                            <div class="radio-box">
                            <input type="radio"  {{ $key==0 ? 'required' : '' }} value="{{ $item->id }}" name="role_id" id="radio-{{ $item->id }}" @if($item->id == (!is_null($admin->role) ? $admin->role->id : 0)) checked @endif>
                            <label for="radio-{{ $item->id }}">{{ $item->name }}</label>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>
            <div class="row cl"></div>
            <div class="row cl _error">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <button type="submit" class="btn btn-success radius" id="admin-role-save" name="admin-role-save"><i class="icon-ok"></i> 确定</button>
                </div>
            </div>
        </form>
    </article>
@endsection

@section('js')
<script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script>
    // 单选框样式
    $('.skin-minimal input').iCheck({
        checkboxClass: 'icheckbox-blue',
        radioClass: 'iradio-blue',
        increaseArea: '20%'
    });

    $("#form-admin-role-add").validate({
        errorPlacement: function (error, element) { //指定错误信息位置
            if (element.is(':radio') || element.is(':checkbox')) { //如果是radio或checkbox
            var eid = element.attr('name'); //获取元素的name属性
            error.appendTo($('._error')); 
            //默认
            // error.appendTo(element.parent()); //将错误信息添加当前元素的父结点后面
            } else {
                error.insertAfter(element);
            }
        },
        messages:{
            role_id:'请选择一个角色'
        },
		onkeyup:false,
		// focusCleanup:true,   //会移除错误提示
		success:"valid",
		submitHandler:function(form){
			$(form).ajaxSubmit({
                success: function(data){
                    if(data.status == 0){
                        layer.msg(data.msg,{icon:1,time:1000});
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        setTimeout(function(){
                            parent.reloadData();    //刷新父页面
                            parent.layer.close(index);
                        },1000);
                    }else if(data.status == -1){
                        $('<div>').appendTo('._error').html('<label id="role_id-error" class="error" for="role_id">'+data.msg+'</label>');
                    }
                }
            });
		}
    });

</script>

@endsection

